package com.stars.pao.springbook.interceptor;


import com.stars.pao.springbook.constants.Constants;
import com.stars.pao.springbook.model.Result;
import com.stars.pao.springbook.model.UserInfo;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

@Component
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //验证用户是否登录
        HttpSession session = request.getSession();
        UserInfo userInfo = (UserInfo) session.getAttribute(Constants.USER_SESSION_KEY);
        if(userInfo == null || userInfo.getId() < 1) {
            response.setStatus(401);
            return false;
        }
        return true;
    }

}
